package com.vishnu.main;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.vishnu.pojo.Address;
import com.vishnu.pojo.Employee;

public class RetrievingOneToManyObjects {

	public static void main(String[] args) {
		Configuration cfg =new Configuration();
		cfg.configure("hib.cfg.xml");
		SessionFactory sf=cfg.buildSessionFactory();
		Session hsession=sf.openSession();
		Transaction tx=hsession.beginTransaction();
	System.out.println("---------transaction started--------");

	Employee employee=new Employee();
	
	employee=(Employee)hsession.get(employee.getClass(),1);
	
	System.out.println("--------------Employee Details-------");
	System.out.println("Id="+employee.getEmployeeId());
	System.out.println("Name="+employee.getEmployeeName());
	System.out.println("Salary="+employee.getEmployeeSalary());
//	System.out.println("Name="+employee.getAddresses());
	Set adresset=employee.getAddresses();
	Iterator it=adresset.iterator();
	int i=1;
	while(it.hasNext()){
		Address address=(Address)it.next();
		System.out.print("Address "+i+"=");
		System.out.print(address.getStreetName()+", ");
		System.out.print(address.getState()+", ");
		System.out.println(address.getCountry());
		i++;
	}
	
	}
}